<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blogs/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blogs/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blogs/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blogs/images/apple-touch-icon.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/blogs/images/favicon-32x32.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/blogs/images/favicon-16x16.png?v=5.1.3">


  <link rel="mask-icon" href="/blogs/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="Vue," />










<meta name="description" content="本文将从以下几个方面描述Vuex  vuex是什么？ 为什么要用vuex? vuex能帮我们解决什么问题？ vuex怎么用？  其中最后一条是重中之重，如何使用vuex 1.vuex是什么？vuex是实现组件全局状态（数据）管理的一种机制，可以方便的实现组件之间的数据共享 vuex具有如下几个核心组件  state: 提供公共数据源，所有的共享数据都要统一放到store里的state中 Mutat">
<meta property="og:type" content="article">
<meta property="og:title" content="Vuex的使用">
<meta property="og:url" content="https://louchaoyy.gitee.io/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/index.html">
<meta property="og:site_name" content="Chao&#39;s blog">
<meta property="og:description" content="本文将从以下几个方面描述Vuex  vuex是什么？ 为什么要用vuex? vuex能帮我们解决什么问题？ vuex怎么用？  其中最后一条是重中之重，如何使用vuex 1.vuex是什么？vuex是实现组件全局状态（数据）管理的一种机制，可以方便的实现组件之间的数据共享 vuex具有如下几个核心组件  state: 提供公共数据源，所有的共享数据都要统一放到store里的state中 Mutat">
<meta property="og:locale">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/1.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/2.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/3.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/4.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/5.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/6.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/7.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/8.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/9.png">
<meta property="article:published_time" content="2022-06-29T14:38:21.000Z">
<meta property="article:modified_time" content="2022-07-01T13:59:59.494Z">
<meta property="article:author" content="Chaoyxy">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://louchaoyy.gitee.io/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/1.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://louchaoyy.gitee.io/blogs/2022/06/29/Vuex的使用/"/>





  <title>Vuex的使用 | Chao's blog</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blogs/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Chao's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blogs/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blogs/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blogs/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://louchaoyy.gitee.io/blogs/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blogs/images/cat.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Chao's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Vuex的使用</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2022-06-29T22:38:21+08:00">
                2022-06-29
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2022/06/29/Vuex的使用/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><strong>本文将从以下几个方面描述Vuex</strong></p>
<ul>
<li>vuex是什么？</li>
<li>为什么要用vuex?</li>
<li>vuex能帮我们解决什么问题？</li>
<li>vuex怎么用？</li>
</ul>
<p><strong>其中最后一条是重中之重，如何使用vuex</strong></p>
<h2 id="1-vuex是什么？"><a href="#1-vuex是什么？" class="headerlink" title="1.vuex是什么？"></a>1.vuex是什么？</h2><p><strong>vuex是实现组件全局状态（数据）管理的一种机制，可以方便的实现组件之间的数据共享</strong></p>
<p>vuex具有如下几个核心组件</p>
<ul>
<li><strong>state: 提供公共数据源，所有的共享数据都要统一放到store里的state中</strong></li>
<li><strong>Mutation: 用于变更store中的数据</strong><ol>
<li> <strong>只能通过mutation去操作和变更store中的数据，不可以直接操作store中的数据</strong></li>
<li> <strong>通过这种方式虽然繁琐一点，但是可集中监控所有数据的变化</strong></li>
</ol>
</li>
<li><strong>module:</strong></li>
<li><strong>action: 可以在这里执行异步任务，但是需要在action中通过触发mutation的方式间接的执行异步操作数据</strong></li>
<li><strong>getter: 用于对store的数据进行加工处理形成新的数据</strong></li>
</ul>
<h2 id="2-为什么要用vuex"><a href="#2-为什么要用vuex" class="headerlink" title="2.为什么要用vuex?"></a>2.为什么要用vuex?</h2><p><strong>组件之间传值：</strong></p>
<ul>
<li><p>父向子传值：v-bind属性绑定</p>
</li>
<li><p>子向父传值：v-on事件绑定</p>
<span id="more"></span></li>
<li><p>兄弟组件传值：EventBus</p>
<ul>
<li>$on 接受数据的组件</li>
<li>$emit发送数据的组件</li>
</ul>
</li>
</ul>
<p>可以看出来由于组件之间的传值很麻烦，很乱，因此引入了vuex对vue数据进行状态管理</p>
<img src="/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/1.png" class title="是否使用vuex之间的区别">

<p><strong>使用vuex统一管理状态的好处</strong></p>
<ol>
<li>能够在vuex中集中管理共享的数据，已于开发和后期维护</li>
<li>能够高效的实现组件之间的数据共享，提高开发效率</li>
<li>存储在vuex中的数据都是响应式的，能够实时保持数据与页面的同步</li>
</ol>
<p><strong>什么样的数据放在vuex中进行共享？</strong></p>
<p>只有组件之间共享的数据才建议放在vuex中，组件的私有数据放在data中即可</p>
<h2 id="3-vuex的使用"><a href="#3-vuex的使用" class="headerlink" title="3.vuex的使用"></a>3.vuex的使用</h2><h3 id="3-1vuex的基本使用"><a href="#3-1vuex的基本使用" class="headerlink" title="3.1vuex的基本使用"></a>3.1vuex的基本使用</h3><h4 id="1-安装vuex依赖包"><a href="#1-安装vuex依赖包" class="headerlink" title="1.安装vuex依赖包"></a>1.<strong>安装vuex依赖包</strong></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install vuex --save</span><br></pre></td></tr></table></figure>

<h4 id="2-导入依赖包"><a href="#2-导入依赖包" class="headerlink" title="2.导入依赖包"></a>2.<strong>导入依赖包</strong></h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> vuex <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span></span><br><span class="line">Vue.use(vuex)</span><br></pre></td></tr></table></figure>

<h4 id="3-创建store对象-vuex-js"><a href="#3-创建store对象-vuex-js" class="headerlink" title="3.创建store对象(vuex.js)"></a>3.<strong>创建store对象</strong>(vuex.js)</h4><p>并且在state中存放了一个count初始值：0</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@name</span>:vuex</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>:louchao</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span>:2022/6/29 23:00</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> Vuex <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span></span><br><span class="line"></span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"><span class="keyword">const</span> store = <span class="keyword">new</span> Vuex.Store(&#123;</span><br><span class="line">    <span class="comment">//数据状态</span></span><br><span class="line">    <span class="attr">state</span>:&#123;</span><br><span class="line">        <span class="attr">count</span>:<span class="number">0</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//</span></span><br><span class="line">    <span class="attr">mutations</span>:&#123;</span><br><span class="line"></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">actions</span>:&#123;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> store;</span><br></pre></td></tr></table></figure>

<h4 id="4-将store对象挂载到vue实例中-main-js"><a href="#4-将store对象挂载到vue实例中-main-js" class="headerlink" title="4.将store对象挂载到vue实例中(main.js)"></a>4.<strong>将store对象挂载到vue实例中</strong>(main.js)</h4><p>将创建的数据共享对象挂载到Vue实例中</p>
<p>所有组件就可以直接从store中使用数据了</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">&#x27;./App.vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> router <span class="keyword">from</span> <span class="string">&#x27;./router/index.js&#x27;</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">&quot;./store/vuex&quot;</span>;</span><br><span class="line"></span><br><span class="line">Vue.config.productionTip = <span class="literal">false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  router,</span><br><span class="line">  <span class="attr">render</span>: <span class="function"><span class="params">h</span> =&gt;</span> h(App),</span><br><span class="line">  store,</span><br><span class="line">&#125;).$mount(<span class="string">&#x27;#app&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h4 id="5-将store中保存的count值取出来"><a href="#5-将store中保存的count值取出来" class="headerlink" title="5.将store中保存的count值取出来"></a>5.<strong>将store中保存的count值取出来</strong></h4><p>提供两种方法</p>
<p><strong>第一：使用<code>this.$store.state.count</code>访问</strong></p>
<p><strong>A组件</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">h2</span>&gt;</span>AddComponent : &#123;&#123;this.$store.state.count&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;addHandler&quot;</span>&gt;</span>加法操作<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">        <span class="attr">name</span>: <span class="string">&quot;AddComaddHandlerponent&quot;</span>,</span></span><br><span class="line"><span class="javascript">        <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">return</span>&#123;&#125;</span></span><br><span class="line"><span class="javascript">        &#125;,</span></span><br><span class="line"><span class="javascript">        <span class="attr">methods</span>:&#123;</span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="title">addHandler</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="built_in">console</span>.log(<span class="string">&quot;++&quot;</span>)</span></span><br><span class="line"><span class="javascript">            &#125;</span></span><br><span class="line"><span class="javascript">        &#125;</span></span><br><span class="line"><span class="javascript">    &#125;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">scoped</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>B组件</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">h2</span>&gt;</span>SubComponent : &#123;&#123;this.$store.state.count&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;subHandler&quot;</span>&gt;</span>减法操作<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">        <span class="attr">name</span>: <span class="string">&quot;SubComponent&quot;</span>,</span></span><br><span class="line"><span class="javascript">        <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">return</span> &#123;&#125;</span></span><br><span class="line"><span class="javascript">        &#125;,</span></span><br><span class="line"><span class="javascript">        <span class="attr">methods</span>:&#123;</span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="title">subHandler</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">            &#125;</span></span><br><span class="line"><span class="javascript">        &#125;,</span></span><br><span class="line"><span class="javascript">    &#125;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">scoped</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<img src="/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/2.png" class title="取到count初始值0">

<p>数据响应式变化</p>
<p>A组件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">methods:&#123;</span><br><span class="line">    <span class="function"><span class="title">addHandler</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.$store.state.count++;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>B组件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">methods:&#123;</span><br><span class="line">    <span class="function"><span class="title">subHandler</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.$store.state.count--;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<img src="/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/3.png" class title="数据响应式变化">

<p><strong>第二：使用计算属性访问</strong></p>
<p><strong>vuex提供了另外一个方法去访问存在store中的数据，就是在计算属性中使用<code>mapState</code>，并且可以使用…展开运算符把拿到的值解析到该vue实例中，相当于放到了data中，便可以使用this.count访问该值</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// computed:&#123;</span></span><br><span class="line"><span class="comment">//     ...mapState([&#x27;count&#x27;]),</span></span><br><span class="line"><span class="comment">// &#125;,</span></span><br><span class="line"><span class="comment">//或者</span></span><br><span class="line"><span class="attr">computed</span>:mapState(&#123;</span><br><span class="line">    <span class="attr">count</span>:<span class="function"><span class="params">state</span>=&gt;</span>state.count,</span><br><span class="line">&#125;),</span><br></pre></td></tr></table></figure>

<img src="/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/4.png" class title="使用mapState访问store中的count">

<h4 id="6-操作store中的值"><a href="#6-操作store中的值" class="headerlink" title="6.操作store中的值"></a>6.操作store中的值</h4><p>为了方便和规范，后续读者将使用计算属性使用mapState对象的方式来取store中的值</p>
<p>使用mutations来进行操作</p>
<p>使用方法，在store中的mutations对象中添加方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> store = <span class="keyword">new</span> Vuex.Store(&#123;</span><br><span class="line">    <span class="comment">//数据状态</span></span><br><span class="line">    <span class="attr">state</span>:&#123;</span><br><span class="line">        <span class="attr">count</span>:<span class="number">0</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//更改数据</span></span><br><span class="line">    <span class="attr">mutations</span>:&#123;</span><br><span class="line">        <span class="comment">//加法</span></span><br><span class="line">        <span class="function"><span class="title">add</span>(<span class="params">state</span>)</span>&#123;</span><br><span class="line">            state.count++;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">//减法,第二个值及以后为操作函数传递的参数</span></span><br><span class="line">        <span class="function"><span class="title">sub</span>(<span class="params">state,value</span>)</span>&#123;</span><br><span class="line">            state.count-=value;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">actions</span>:&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在组件中使用</p>
<p><strong>第一种方式是使用<code>this.$store.commit(&#39;add&#39;)</code>，第一个参数为store中的mutations中的方法名，第二个参数为要传递的参数</strong></p>
<p><strong>A组件</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">    <span class="keyword">import</span> &#123;mapState&#125; <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span>;</span><br><span class="line">    <span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&quot;AddComaddHandlerponent&quot;</span>,</span><br><span class="line">        <span class="comment">// computed:&#123;</span></span><br><span class="line">        <span class="comment">//     ...mapState([&#x27;count&#x27;]),</span></span><br><span class="line">        <span class="comment">// &#125;,</span></span><br><span class="line">        <span class="comment">//或者</span></span><br><span class="line">        <span class="attr">computed</span>:mapState(&#123;</span><br><span class="line">            <span class="attr">count</span>:<span class="function"><span class="params">state</span>=&gt;</span>state.count,</span><br><span class="line">        &#125;),</span><br><span class="line">        <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="keyword">return</span>&#123;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="attr">methods</span>:&#123;</span><br><span class="line">            <span class="function"><span class="title">addHandler</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">                <span class="built_in">this</span>.$store.commit(<span class="string">&#x27;add&#x27;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p><strong>B组件</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">    <span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&quot;SubComponent&quot;</span>,</span><br><span class="line">        <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="keyword">return</span> &#123;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="attr">methods</span>:&#123;</span><br><span class="line">            <span class="function"><span class="title">subHandler</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">                <span class="built_in">this</span>.$store.commit(<span class="string">&#x27;sub&#x27;</span>,<span class="number">3</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p>效果图</p>
<img src="/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/5.png" class title="加法操作">

<img src="/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/6.png" class title="减法操作">

<p><strong>第二种方式是使用mapMutations，用法跟mapState类似</strong></p>
<p>使用步骤</p>
<p><strong>先按需导入mapMutations，然后再method中进行展开运算符，映射为当前method中的函数，就相当于method中的函数，就可以用this.functionName()进行访问</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//A组件</span></span><br><span class="line">&lt;script&gt;</span><br><span class="line">    <span class="keyword">import</span> &#123;mapState,mapMutations&#125; <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span>;</span><br><span class="line">    <span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&quot;AddComaddHandlerponent&quot;</span>,</span><br><span class="line">        <span class="comment">// computed:&#123;</span></span><br><span class="line">        <span class="comment">//     ...mapState([&#x27;count&#x27;]),</span></span><br><span class="line">        <span class="comment">// &#125;,</span></span><br><span class="line">        <span class="comment">//或者</span></span><br><span class="line">        <span class="attr">computed</span>:mapState(&#123;</span><br><span class="line">            <span class="attr">count</span>:<span class="function"><span class="params">state</span>=&gt;</span>state.count,</span><br><span class="line">        &#125;),</span><br><span class="line">        <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="keyword">return</span>&#123;</span><br><span class="line">                </span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="attr">methods</span>:&#123;</span><br><span class="line">             <span class="comment">//在methos中进行映射</span></span><br><span class="line">            ...mapMutations([<span class="string">&#x27;add&#x27;</span>,<span class="string">&#x27;sub&#x27;</span>]),</span><br><span class="line">            <span class="function"><span class="title">addHandler</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">                <span class="comment">// this.$store.commit(&#x27;add&#x27;);</span></span><br><span class="line">                <span class="built_in">this</span>.add();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"><span class="comment">//B组件</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span></span><br><span class="line"><span class="javascript"><span class="xml">    <span class="keyword">import</span> &#123;mapMutations&#125; <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span>;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">    <span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">        <span class="attr">name</span>: <span class="string">&quot;SubComponent&quot;</span>,</span></span></span><br><span class="line"><span class="javascript"><span class="xml">        <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">            <span class="keyword">return</span> &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">            &#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">        &#125;,</span></span></span><br><span class="line"><span class="javascript"><span class="xml">        <span class="attr">methods</span>:&#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">            <span class="comment">//在methos中进行映射</span></span></span></span><br><span class="line"><span class="javascript"><span class="xml">            ...mapMutations([<span class="string">&#x27;sub&#x27;</span>]),</span></span></span><br><span class="line"><span class="javascript"><span class="xml">            <span class="function"><span class="title">subHandler</span>(<span class="params"></span>)</span>&#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">                <span class="comment">// this.$store.commit(&#x27;sub&#x27;,3);</span></span></span></span><br><span class="line"><span class="javascript"><span class="xml">                <span class="comment">//正常传参</span></span></span></span><br><span class="line"><span class="javascript"><span class="xml">                <span class="built_in">this</span>.sub(<span class="number">2</span>);</span></span></span><br><span class="line"><span class="javascript"><span class="xml">            &#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">        &#125;,</span></span></span><br><span class="line"><span class="javascript"><span class="xml">    &#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

<p>执行效果如上图所示</p>
<p><strong>注意：在mutations中不能执行异步操作，异步操作应该放在Action中去执行</strong></p>
<h4 id="7-Action执行异步操作"><a href="#7-Action执行异步操作" class="headerlink" title="7.Action执行异步操作"></a>7.Action执行异步操作</h4><p><strong>action:可以在这里执行异步任务，但是需要在action中通过触发mutation的方式间接的执行异步操作数据</strong></p>
<p>使用：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> store = <span class="keyword">new</span> Vuex.Store(&#123;</span><br><span class="line">    <span class="comment">//数据状态</span></span><br><span class="line">    <span class="attr">state</span>:&#123;</span><br><span class="line">        <span class="attr">count</span>:<span class="number">0</span>,</span><br><span class="line">        <span class="attr">number</span>:<span class="number">0</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//更改数据</span></span><br><span class="line">    <span class="attr">mutations</span>:&#123;</span><br><span class="line">        <span class="comment">//加法</span></span><br><span class="line">        <span class="function"><span class="title">add</span>(<span class="params">state</span>)</span>&#123;</span><br><span class="line">            state.count++;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">//减法,第二个值及以后为操作函数传递的参数</span></span><br><span class="line">        <span class="function"><span class="title">sub</span>(<span class="params">state,value</span>)</span>&#123;</span><br><span class="line">            state.count-=value;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">//加5操作</span></span><br><span class="line">        <span class="function"><span class="title">addAny</span>(<span class="params">state,value</span>)</span>&#123;</span><br><span class="line">            state.number+=value;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&quot;2秒之后值变为&quot;</span>,value);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">actions</span>:&#123;</span><br><span class="line">        <span class="comment">//异步任务</span></span><br><span class="line">        <span class="function"><span class="title">addAsync</span>(<span class="params">context,val</span>)</span>&#123;</span><br><span class="line">            <span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">                context.commit(<span class="string">&#x27;addAny&#x27;</span>,val);</span><br><span class="line">            &#125;,<span class="number">2000</span>);</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p><strong>触发action中的函数</strong></p>
<p>使用this.$store.dispatch()来进行触发</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">    <span class="keyword">import</span> &#123;mapState,mapMutations,mapActions&#125; <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span>;</span><br><span class="line">    <span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&quot;AddComaddHandlerponent&quot;</span>,</span><br><span class="line">        <span class="comment">// computed:&#123;</span></span><br><span class="line">        <span class="comment">//     ...mapState([&#x27;count&#x27;]),</span></span><br><span class="line">        <span class="comment">// &#125;,</span></span><br><span class="line">        <span class="comment">//或者</span></span><br><span class="line">        <span class="attr">computed</span>:mapState(&#123;</span><br><span class="line">            <span class="attr">count</span>:<span class="function"><span class="params">state</span>=&gt;</span>state.count,</span><br><span class="line">            <span class="attr">number</span>:<span class="function"><span class="params">state</span>=&gt;</span>state.number,</span><br><span class="line">        &#125;),</span><br><span class="line">        <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="keyword">return</span>&#123;</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="attr">methods</span>:&#123;</span><br><span class="line">            ...mapMutations([<span class="string">&#x27;add&#x27;</span>,<span class="string">&#x27;sub&#x27;</span>]),</span><br><span class="line">            <span class="comment">//映射到methods方法中</span></span><br><span class="line">            ...mapActions([<span class="string">&#x27;addAsync&#x27;</span>]),</span><br><span class="line">            <span class="function"><span class="title">addHandler</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">                <span class="comment">// this.$store.commit(&#x27;add&#x27;);</span></span><br><span class="line">                <span class="built_in">this</span>.add();</span><br><span class="line">            &#125;,</span><br><span class="line">            <span class="function"><span class="title">addHandler1</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">                <span class="comment">//第一种方法触发action里面的函数</span></span><br><span class="line">                <span class="built_in">this</span>.$store.dispatch(<span class="string">&#x27;addAsync&#x27;</span>,<span class="number">5</span>);</span><br><span class="line">                <span class="comment">//第二种方法触发action里面的函数</span></span><br><span class="line">                <span class="built_in">this</span>.addAsync(<span class="number">5</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<img src="/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/7.png" class title="执行异步操作">

<img src="/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/8.png" class title="执行异步操作">

<h4 id="8-getter的使用"><a href="#8-getter的使用" class="headerlink" title="8.getter的使用"></a>8.getter的使用</h4><p>getter用于对store的数据进行加工处理形成新的数据</p>
<ul>
<li>getter 可以对store中已有的数据加工处理后形成新数据，类似于Vue中的计算属性</li>
<li>store中 的数据变化，getter中的数据也会发生变化</li>
</ul>
<p>在store中定义</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> store = <span class="keyword">new</span> Vuex.Store(&#123;</span><br><span class="line">    <span class="comment">//数据状态</span></span><br><span class="line">    <span class="attr">state</span>:&#123;</span><br><span class="line">        <span class="attr">count</span>:<span class="number">0</span>,</span><br><span class="line">        <span class="attr">number</span>:<span class="number">0</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//更改数据</span></span><br><span class="line">    <span class="attr">mutations</span>:&#123;</span><br><span class="line">        <span class="comment">//加法</span></span><br><span class="line">        <span class="function"><span class="title">add</span>(<span class="params">state</span>)</span>&#123;</span><br><span class="line">            state.count++;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">//减法,第二个值及以后为操作函数传递的参数</span></span><br><span class="line">        <span class="function"><span class="title">sub</span>(<span class="params">state,value</span>)</span>&#123;</span><br><span class="line">            state.count-=value;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">//加5操作</span></span><br><span class="line">        <span class="function"><span class="title">addAny</span>(<span class="params">state,value</span>)</span>&#123;</span><br><span class="line">            state.number+=value;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&quot;2秒之后值变为&quot;</span>,value);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">actions</span>:&#123;</span><br><span class="line">        <span class="comment">//异步任务</span></span><br><span class="line">        <span class="function"><span class="title">addAsync</span>(<span class="params">context,val</span>)</span>&#123;</span><br><span class="line">            <span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">                context.commit(<span class="string">&#x27;addAny&#x27;</span>,val);</span><br><span class="line">            &#125;,<span class="number">2000</span>);</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//包装数据</span></span><br><span class="line">    <span class="attr">getters</span>:&#123;</span><br><span class="line">        <span class="function"><span class="title">shown</span>(<span class="params">state</span>)</span>&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&#x27;当前最新的数量是&#x27;</span>+state.count;</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>访问：</p>
<p><strong>第一种方式</strong></p>
<p><strong>在组件中通过<code>this.$store.getters.名称</code>的形式访问</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">h2</span>&gt;</span>AddComponent : &#123;&#123;count&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">h3</span>&gt;</span>Add any async: &#123;&#123;number&#125;&#125;<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">h4</span>&gt;</span>&#123;&#123;$store.getters.shown&#125;&#125;<span class="tag">&lt;/<span class="name">h4</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;addHandler&quot;</span>&gt;</span>加法操作<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;addHandler1&quot;</span>&gt;</span>异步加法操作<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>第二种方式：</strong></p>
<p><strong>在计算属性中去访问</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123;mapState,mapMutations,mapActions,mapGetters&#125; <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    <span class="attr">name</span>: <span class="string">&quot;AddComaddHandlerponent&quot;</span>,</span><br><span class="line">    <span class="attr">computed</span>:&#123;</span><br><span class="line">        ...mapState([<span class="string">&#x27;count&#x27;</span>,<span class="string">&#x27;number&#x27;</span>]),</span><br><span class="line">        ...mapGetters([<span class="string">&#x27;shown&#x27;</span>]),</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//或者</span></span><br><span class="line">    <span class="comment">// computed:mapState(&#123;</span></span><br><span class="line">    <span class="comment">//     count:state=&gt;state.count,</span></span><br><span class="line">    <span class="comment">//     number:state=&gt;state.number,</span></span><br><span class="line">    <span class="comment">// &#125;),</span></span><br><span class="line">    <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>&#123;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>如图：</strong></p>
<img src="/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/9.png" class title="最新的count值（在getter中的）">

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/alipay.jpg" alt=" 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/wechatpay.jpg" alt=" 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blogs/tags/Vue/" rel="tag"># Vue</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blogs/2022/06/28/vue%E7%BB%84%E4%BB%B6%E4%B9%8B%E9%97%B4%E7%9A%84%E4%BC%A0%E5%80%BC/" rel="next" title="vue组件之间的传值">
                <i class="fa fa-chevron-left"></i> vue组件之间的传值
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blogs/2022/07/01/pinia%E7%9A%84%E4%BD%BF%E7%94%A8/" rel="prev" title="pinia的使用">
                pinia的使用 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a target="_blank" rel="noopener" href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blogs/images/cat.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blogs/archives">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blogs/tags/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/louchaoyy" target="_blank" title="Gitee">
                      
                        <i class="fa fa-fw fa-gitee"></i>Gitee</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="louchao@louchaoyy.cn" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-vuex%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="nav-number">1.</span> <span class="nav-text">1.vuex是什么？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E7%94%A8vuex"><span class="nav-number">2.</span> <span class="nav-text">2.为什么要用vuex?</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-vuex%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-number">3.</span> <span class="nav-text">3.vuex的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1vuex%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="nav-number">3.1.</span> <span class="nav-text">3.1vuex的基本使用</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-%E5%AE%89%E8%A3%85vuex%E4%BE%9D%E8%B5%96%E5%8C%85"><span class="nav-number">3.1.1.</span> <span class="nav-text">1.安装vuex依赖包</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-%E5%AF%BC%E5%85%A5%E4%BE%9D%E8%B5%96%E5%8C%85"><span class="nav-number">3.1.2.</span> <span class="nav-text">2.导入依赖包</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-%E5%88%9B%E5%BB%BAstore%E5%AF%B9%E8%B1%A1-vuex-js"><span class="nav-number">3.1.3.</span> <span class="nav-text">3.创建store对象(vuex.js)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-%E5%B0%86store%E5%AF%B9%E8%B1%A1%E6%8C%82%E8%BD%BD%E5%88%B0vue%E5%AE%9E%E4%BE%8B%E4%B8%AD-main-js"><span class="nav-number">3.1.4.</span> <span class="nav-text">4.将store对象挂载到vue实例中(main.js)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-%E5%B0%86store%E4%B8%AD%E4%BF%9D%E5%AD%98%E7%9A%84count%E5%80%BC%E5%8F%96%E5%87%BA%E6%9D%A5"><span class="nav-number">3.1.5.</span> <span class="nav-text">5.将store中保存的count值取出来</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-%E6%93%8D%E4%BD%9Cstore%E4%B8%AD%E7%9A%84%E5%80%BC"><span class="nav-number">3.1.6.</span> <span class="nav-text">6.操作store中的值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#7-Action%E6%89%A7%E8%A1%8C%E5%BC%82%E6%AD%A5%E6%93%8D%E4%BD%9C"><span class="nav-number">3.1.7.</span> <span class="nav-text">7.Action执行异步操作</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#8-getter%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-number">3.1.8.</span> <span class="nav-text">8.getter的使用</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Chaoyxy</span>

  
</div>

<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>
-->




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/blogs/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/blogs/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/blogs/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/blogs/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/blogs/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/blogs/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://Chaoyxy.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'https://louchaoyy.gitee.io/blogs/2022/06/29/Vuex%E7%9A%84%E4%BD%BF%E7%94%A8/';
          this.page.identifier = '2022/06/29/Vuex的使用/';
          this.page.title = 'Vuex的使用';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://Chaoyxy.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
